//统计页面跳转到详细页面
var showDriverBehaviorDetail;
//统计页面传递过来的参数
var passParams;
//跳转到车辆监控
var busMonitor;
//详细地址
var locationDetailInfoDialog;
var showLocationDetailInfo;
var itemSearch;
var driverBehaviorWarn;
//跳转到车辆信息页面
var goCarInfo;
//跳转到线路信息页面
var goLineInfo;
//跳转到驾驶员信息页面
var godrvAnaDriverInfo;

var captainTodaySearch;

require(['grid', 'topBar', 'layout', 'date'], function () {
    $('#top').topBar({
        left: [
            {
                type: 'filter-more',
                dateType:'custom',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name1: 'beginTime',
                        name2: 'endTime',
                        type: 'dateLine'
                    }, {
                        text: '异常行为',
                        url: "/drivingBehavior/getBhvDsc.htm",
                        name: 'drvsctbhv',
                        type: 'item-select',
                        key:'fieldCodeValue',
                        value:'fieldCodeDsc',
                        data: [
                            {text: '全部', value: ''}
                        ]
                    }
                ]
            }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '驾驶员行为位置查询',
                type: 'btn',
                click: function () {
                    var ids = $('#grid').grid("getGridParam", "selarrrow");
                    if (ids && ids.length > 0) {
                        var data = new Array();  // 在这里构造车辆数据的数组
                        for (var i = 0; i < ids.length; i++) {
                            var rowData = $('#grid').grid("getRowData", ids[i]);
                            var obj = new Object();  //车辆数据
                            obj.licPltNo = rowData.licPltNo; //车牌号
                            obj.driverName = rowData.drvName; //驾驶员
                            obj.loLgt = rowData.beginLoLgt; //经度
                            obj.loLtt = rowData.beginLoLtt; //纬度
                            obj.direction = rowData.beginLoDrc; //方向
                            obj.type = rowData.drvSctBhv;
                            ; //异常类型 字符串表示
                            obj.beginTime = rowData.sctBhvBeginTime; //开始时间
                            obj.endTime = rowData.sctBhvEndTime; //结束时间
                            obj.beginTime = rowData.sctBhvBeginTime; //开始时间
                            obj.endTime = rowData.sctBhvEndTime; //结束时间
                            obj.corpId = rowData.corpId; //机构
                            obj.lineId = rowData.lineId; //线路
                            data.push(obj);
                        }
                        openLocationDetailInfoDialog("位置信息查询", data); //弹出的标题和数据
                    } else {
                        $.messager.alert("警告", "没有数据。", "info");
                    }
                }
            }
        ]
    });

    $('body').layout();

    //显示驾驶员行为详情
    showDriverBehaviorDetail = function (type, orgIds, beginTime, endTime, drvsctbhv) {
        passParams = {
            "type": type,
            "orgIds": orgIds,
            "beginTime": beginTime,
            "endTime": endTime,
            "drvsctbhv": drvsctbhv
        };
        if(type=="LINE"){
        	//切换成线路
			commonFun.showLeftTree(commonFun.lineTree);
			//勾选并展开节点
			commonFun.setCheckedNodes(orgIds);
        }
        else if(type=='DRIVER'){
        	//切换成驾驶员
			commonFun.showLeftTree(commonFun.driverTree);
			//勾选并展开节点
			commonFun.setCheckedNodes(orgIds);
        }
        $('.topBar').find('[name=beginTime]').val(beginTime);
        $('.topBar').find('[name=endTime]').val(endTime);
        $('.topBar').find('[name=drvsctbhv] a').each(function () {
            $(this).attr('class', '');
            var val = $(this).attr('data-val');
            if (val == drvsctbhv) {
                $(this).attr('class', 'active');
            }
        });
        $('#grid').grid('setGridParam', {
            page: 1,
            datatype: "json",
            postData: passParams
        }).trigger("reloadGrid");
    }

    busMonitor = function (car_id, carType, carLic) {
        var data = {};
        data.carId = car_id;
        $.openTab('bf0c2ab0b9a14bb7a09a3d7fa42da459', 'checkSelCar', [data]);
        // $.openTab('bf0c2ab0b9a14bb7a09a3d7fa42da459', 'checkSelCar', '');
    }
    //显示 详细位置信息
    showLocationDetailInfo = function (licPltNo, drvName, beginLoLgt, beginLoLtt, beginLoDrc, type, sctBhvBeginTime, sctBhvEndTime, corpId, lineId) {
        var data = new Array();  // 在这里构造车辆数据的数组
        var obj = new Object();  //车辆数据
        obj.licPltNo = licPltNo; //车牌号
        obj.driverName = drvName; //驾驶员
        obj.loLgt = beginLoLgt; //经度
        obj.loLtt = beginLoLtt; //纬度
        obj.direction = beginLoDrc; //方向
        obj.type = type; //异常类型 字符串表示
        obj.beginTime = sctBhvBeginTime; //开始时间
        obj.endTime = sctBhvEndTime; //结束时间
        obj.corpId = corpId; //开始时间
        obj.lineId = lineId; //结束时间
        data.push(obj);
        openLocationDetailInfoDialog("位置信息查询", data); //弹出的标题和数据
    };

    function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
                url: './common/map/mapLocation.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }

    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") > 0) {
            var url = "/drivingBehavior/exportDrivingBehavior2" + suffix + ".htm";
            if (passParams) {
                $.exportFile(url, passParams);
            }
            else {
                var type = commonFun.getLeftTreeCurrentTreeType();
                var orgIds = commonFun.getLeftTreeCheckedNodes(type);
                if (!orgIds) {
                    $.messager.alert("提示", "请选择查询车辆/机构/驾驶员。", "info");
                } else {
                    var $postData = $("#top").parent().getValues();
                    $postData.type = type;
                    $postData.orgIds = orgIds;
                    $.exportFile(url, $postData);
                }
            }
        }
        else {
            $.messager.alert("提示", "请先查询数据。", "info");
        }
    }

    $('#grid').grid({
        url: "/drivingBehavior/searchDrivingBehavior.htm",
        colModel: [
            {name: "corpId", label: "corpId", hidden: true},
            {name: "lineId", label: "lineId", hidden: true},
            {name: "rcrdId", label: "rcrdId", key: true, hidden: true},
            {name: "carId", label: "carId", hidden: true},
            {name: "carType", label: "carType", hidden: true},
            {name: "beginLoLgt", label: "beginLoLgt", hidden: true},
            {name: "beginLoLtt", label: "beginLoLtt", hidden: true},
            {name: "beginLoDrc", label: "beginLoDrc", hidden: true},
            {name: "drvName", label: "驾驶员",width:80,formatter: 'customCurrency',formatoptions:{
                redbtn:true,
                css:'text-link c-blue',
                clickFn:function(rowObject,cellvalue){
                	godrvAnaDriverInfo(rowObject.drvId);
                }
            }},
            {name: "licPltNo", label: "车牌号",width:80,formatter: function (cellvalue,options,rowObject) {
            	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
            }},
            {name: "carInCd", label: "车辆编号",width:80},
            {name: "lineName", label: "营运线路",width:80,formatter: 'customCurrency',formatoptions:{
                redbtn:true,
                css:'text-link c-blue',
                clickFn:function(rowObject,cellvalue){
                	goLineInfo(rowObject.lineId);
                }
            }},
            {name: "organName", label: "所属机构",width:80},
            {name: "drvSctBhv", label: "异常行为",width:80},
            {name: "duration", label: "持续时长",width:80},
            {name: "reservedField1",label: "车速(km/h)",width:100},
            {name: "reservedField2",label: "转速(r/min)",width:100},
            {name: "sctBhvBeginTime", label: "发生时间",width:80},
            {name: "sctBhvEndTime", label: "结束时间",hidden: true},
            {name: "happenlocation", label: "发生站点",width:80,
            	formatter: function (cellvalue, options, rowObject) {
                    var address;
                    if (rowObject.address == null | rowObject.address == "" | typeof(rowObject.address) == "undefined") {
                        address = "未知地址";
                    } else {
                        address = rowObject.address;
                    }
                    var licPltNo = rowObject.licPltNo||"";
                    var drvName = rowObject.drvName||"";
                    var beginLoLgt = rowObject.beginLoLgt;
                    var beginLoLtt = rowObject.beginLoLtt;
                    var beginLoDrc = rowObject.beginLoDrc;
                    var drvSctBhv = rowObject.drvSctBhv;
                    var sctBhvBeginTime = rowObject.sctBhvBeginTime;
                    var sctBhvEndTime = rowObject.sctBhvEndTime;
                    var corpId = rowObject.corpId;
                    var lineId = rowObject.lineId;
                    var content = "<b><a href='javascript:void(0);' onclick='showLocationDetailInfo(\"" +
                        licPltNo + "\",\"" +
                        drvName + "\",\"" +
                        beginLoLgt + "\",\"" +
                        beginLoLtt + "\",\"" +
                        beginLoDrc + "\",\"" +
                        drvSctBhv + "\",\"" +
                        sctBhvBeginTime + "\",\"" +
                        sctBhvEndTime + "\",\"" +
                        corpId + "\",\"" +
                        lineId + "\")' style='color: blue;'>" + cellvalue + "</a></b>";
                    return content;
                }
            },
            {
                name: "busMonitor", label: "车辆监控",width:80,
                formatter: function (cellvalue, options, rowObject) {
                    var content = "<a href ='javascript:;' class='dia_gj' onclick='busMonitor(\"" + rowObject.carId + "\",\"" + rowObject.carType + "\",\"" + rowObject.licPltNo + "\")'>"
                        + "<img src ='../../content/images/car.png'/>" + "</a>";
                    return content;
                }
            }
        ],
        gridComplete : function() {
            var rowIds = $('#grid').jqGrid('getDataIDs');
            var cells = $('#grid').find('td[aria-describedby="grid_happenlocation"]');
			$.each(cells, function(i, obj){
				var title = $('#grid').jqGrid('getCell', rowIds[i], 6)+","+$('#grid').jqGrid('getCell', rowIds[i], 7);
				$(obj).attr('title', title);
			});
        }
    });


    function search() {
        passParams = null;
        var $postData = $("#top").parent().getValues();
        // 左侧树tab选中的类型
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (!orgIds) {
            $.messager.alert("提示", "请选择查询车辆/机构/驾驶员！", "info");
        } else {
            $postData.type = type;
            $postData.orgIds = orgIds.toString();
            $('#grid').grid('setGridParam', {
                datatype: "json",
                postData: $postData
            }).trigger("reloadGrid");
        }
    }
    
    itemSearch = function(data){
    	$('#grid').grid('setGridParam', {
            datatype: "json",
            postData: {
            	"orgIds": data.lineId,
            	"type": "LINE",
            	"beginTime": data.today,
            	"endTime": data.today + " 23:59:59"
            }
        }).trigger("reloadGrid");
    }
    
    driverBehaviorWarn = function(){
    	var today = (new Date()).format();
    	var Ids = [];
    	interval = self.setInterval(function(){
    		var node = commonFun.getNodesByType("CAR");
			if(node && node.length>0){
				clearInterval(interval);
				$.each(node, function(index, item){
		    		Ids.push(item.id);
		    	});
				$('#grid').grid('setGridParam', {
		            page:1,
		            datatype: "json",
		            postData: {
		            	"orgIds": Ids,
		            	"type": "CAR",
		            	"beginTime": today,
		            	"endTime": today + " 23:59:59"
		            }
		        }).trigger("reloadGrid");
			}
		},1000);
    }

    //跳转到驾驶员信息页面
    godrvAnaDriverInfo = function(drvId){
    	var data = {};
    	data.drvId = drvId;
    	$.openTab('8252b8b5e5ff49c08b02f6865562b506','showDrvInfo',[data]);
    };
    //跳转到车辆信息页面
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }

    captainTodaySearch = function(data){
        //切换成线路
        commonFun.showLeftTree(commonFun.lineTree);
        //勾选并展开节点
        commonFun.setCheckedNodes(data.lineIds);
        $("div[name='drvsctbhv']").find("a[data-val='']").click();
        $("a[data-val='01']").click();
    };

})
